package nl.infodation.emr.pricingservice.session;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import nl.infodation.emr.pricingservice.ui.page.PricingServiceApplication;
import nl.infodation.emr.utility.Constants;

public class SessionListener implements HttpSessionListener {
	private static String contextPath = null;
	
	public SessionListener() {
	}
	
	public void sessionCreated(HttpSessionEvent sessionEvent) {
		if (SessionListener.contextPath == null) {
			SessionListener.contextPath = sessionEvent.getSession().getServletContext().getRealPath("/");
		}
	}
	
	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
		// Get the session that was invalidated
		HttpSession session = sessionEvent.getSession();
		// Log a message
		String currentUsername = (String)session.getAttribute(Constants.CURRENT_USER_SESSION);
		PricingServiceApplication.removeUser(currentUsername);
	}
	
	public static String getContextPath() {
		return SessionListener.contextPath;
	}
}
